#include "stm32f10x.h"
#include "USART.h"


u8 RxBuffer[RxBufferSize];
u8 RxBuffer485[RxBufferSize];

u16 RxCounter = 0;
u16 RxCounter485 = 0; 
u8 flag485 = 0;
u8 flagRFID = 0;
u8 rfid[4];

void USART1_SendByte(u16 Data)
{ 
   while (!(USART1->SR & USART_FLAG_TXE));
   USART1->DR = (Data & (uint16_t)0x01FF);	 
   
}
void USART1Write(u8* data,u16 len)
{
	u16 i;
//	DIR485_H ;
	for (i=0; i<len; i++){
		USART1_SendByte(data[i]);
	}
//	Delay10us(1000);
//	DIR485_L;		
}	
void USART1_Configuration(void)
{
	USART_InitTypeDef USART_InitStructure;
	USART_InitStructure.USART_BaudRate            = 9600  ;
	USART_InitStructure.USART_WordLength          = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits            = USART_StopBits_1;
	USART_InitStructure.USART_Parity              = USART_Parity_No ;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode                = USART_Mode_Rx | USART_Mode_Tx;
	USART_Init(USART1, &USART_InitStructure);
	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
	USART_Cmd(USART1, ENABLE);     
/*	DIR485_L;  */
}

void USART1_IRQHandler(void)
{

	if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
  	{
    /* Read one byte from the receive data register */
   		RxBuffer[RxCounter++] = (USART_ReceiveData(USART1) & 0xFF);          

    /* Clear the USART1 Receive interrupt */
    	USART_ClearITPendingBit(USART1, USART_IT_RXNE);

		rfid[0] = RxBuffer[1];
		rfid[1] = RxBuffer[2];
		rfid[2] = RxBuffer[3];
		rfid[3] = RxBuffer[4];

		GPIO_ResetBits(GPIOF, GPIO_Pin_6 );
		flagRFID = 1;

		if(RxCounter >= 7)
		{
			RxCounter = 0;
		}
		if(RxBuffer[0] != 0x02)
		{
			RxCounter = 0;
		} 
	}
/*	u8 c;
	if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
	{ 	
	    c=USART1->DR;
		USART_ClearITPendingBit(USART1, USART_IT_RXNE);
		USART1_SendByte(c); 	    
	} 
*/	 
}

void USART3_SendByte(u16 Data)
{ 
   while (!(USART3->SR & USART_FLAG_TXE));
   USART3->DR = (Data & (uint16_t)0x01FF);	 
   
}
void USART3Write(u8* data,u16 len)
{
	u16 i;
	DIR485_H ;
	for (i=0; i<len; i++){
		USART3_SendByte(data[i]);
	}
//	Delay10us(1000);
	DIR485_L;		
}	
void USART3_Configuration(void)
{
	USART_InitTypeDef USART_InitStructure;
	USART_InitStructure.USART_BaudRate            = 115200  ;
	USART_InitStructure.USART_WordLength          = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits            = USART_StopBits_1;
	USART_InitStructure.USART_Parity              = USART_Parity_No ;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode                = USART_Mode_Rx | USART_Mode_Tx;
	USART_Init(USART3, &USART_InitStructure);
	USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
	USART_Cmd(USART3, ENABLE);     
	DIR485_L;  
}

void USART3_IRQHandler(void)
{
	if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
  	{
    /* Read one byte from the receive data register */
   		RxBuffer485[RxCounter485++] = (USART_ReceiveData(USART3) & 0xFF);          

    /* Clear the USART1 Receive interrupt */
    	USART_ClearITPendingBit(USART3, USART_IT_RXNE);
		if(RxCounter485 >= 5)
		{
			flag485 = 1;
			RxCounter485 = 0;
			USART_ITConfig(USART3, USART_IT_RXNE, DISABLE);
		}
		if(RxBuffer485[0] != 0x55)
		{
			RxCounter485 = 0;
		} 
	}
/*	u8 c;
	if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
	{ 	
	    c=USART3->DR;
		USART3_SendByte(c); 	    
	} 
*/	 
}

void Delay(vu32 nCount)
{
  for(; nCount != 0; nCount--);
}



